import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { UserService } from '../../services/user.service';
import { User } from '../../entity/User';
import { Route } from '@angular/compiler/src/core';
import { Router, ActivatedRoute } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import { Examination } from '../../entity/Examination';
import { isatty } from 'tty';

@Component({
  selector: 'app-taoti-detail',
  templateUrl: './taoti-detail.component.html',
  styleUrls: ['./taoti-detail.component.css']
})
export class TaotiDetailComponent implements OnInit {
  data = [];
  id: number;
  constructor(private http: HttpClient
    , private router: Router
    , private message: NzMessageService
    , private route: ActivatedRoute) {
  }

  ngOnInit() {
    this.Search();
  }

  Search() {
    this.route.params.subscribe((params) => {
      this.id = params.id;
      this.http
        .get(`/api/OnlyQuestions/getAllByTaotiID?id=${this.id}`)
        .subscribe(response => {
          (<Array<Examination>>response).forEach((value, index) => { value.type = '单选'; });
          this.data = this.data.concat(response);
          this.http
            .get(`/api/ServeralQuestions/getAllByTaotiID?id=${this.id}`)
            .subscribe(response2 => {
              (<Array<Examination>>response2).forEach((value, index) => { value.type = '多选'; });
              this.data = this.data.concat(response2);
              this.http
                .get(`/api/FillingQuestions/getAllByTaotiID?id=${this.id}`)
                .subscribe(response3 => {
                  (<Array<Examination>>response3).forEach((value, index) => { value.type = '填空'; });
                  this.data = this.data.concat(response3);
                  this.http
                    .get(`/api/ProgrammerQuestions/getAllByTaotiID?id=${this.id}`)
                    .subscribe(response4 => {
                      (<Array<Examination>>response4).forEach((value, index) => { value.type = '编程'; });
                      this.data = this.data.concat(response4);
                    });
                });
            });
        });
    });
  }

  New(type) {
    this.router.navigate([`content/alterTaotiDetail/${this.id}/${type}/0`]);
  }

  Alter(id, type) {
    this.router.navigate([`content/alterTaotiDetail/${this.id}/${type}/${id}`]);
  }

  Delete(id, type) {
    let delUrl = '';
    if (type == '单选') {
      delUrl = `/api/OnlyQuestions/delete`;
    } else if (type == '多选') {
      delUrl = `/api/ServeralQuestions/delete`;
    } else if (type == '填空') {
      delUrl = `/api/FillingQuestions/delete`;
    } else if (type == '编程') {
      delUrl = `/api/ProgrammerQuestions/delete`;
    }
    this.http
      .get(
        `${delUrl}?id=${id}`
      ).subscribe(response => {
        this.message.info('删除成功');
        this.Search();
      });
  }
}
